const mongoose = require('mongoose')
const ObjectId = mongoose.Types.ObjectId
const { Admin } = require('../../schema')

mongoose.connect('mongodb://127.0.0.1:27017/sportswear').then()
const adminCollection = mongoose.model('admin', Admin)

function getAdminList () {
    return new Promise(resolve => {
        adminCollection.find({}).then(res => {
            resolve(res)
        })
    })
}

function insertAdmin ({ adminname, password, type, author }) {
    const record = new adminCollection({ adminname, password, type, author, freeze: false })
    return new Promise((resolve, reject) => {
        adminCollection.findOne({ adminname }).then(res => {
            if (res) {
                reject('已经存在改管理员')
            } else {
                record.save().then(res => {
                    resolve('管理员添加成功')
                })
            }
        })
    })
}

function freezeAdmin (_id, bool) {
    return new Promise(resolve => {
        adminCollection.updateOne({ _id }, {
            freeze: bool
        }).then(res => {
            if (res.modifiedCount) {
                resolve('管理员冻结状态变更')
            } else {
                resolve('管理员冻结状态没有改变')
            }
        }).catch(err => {
            resolve('管理员冻结失败')
        })
    })
}

function modifyAdmin (_id, data) {
    return new Promise(resolve => {
        let _data = {
            adminname: undefined,
            password: undefined,
            freeze: undefined,
            type:undefined,
            author:undefined
        }
        _data = Object.assign(_data, data)
        for (let key in _data) {
            if (_data[key] === undefined) {
                delete _data[key]
            }
        }
        adminCollection.findOne({ adminname: _data.adminname }).then(res => {
            if (!res || res._id.toString() === _id) {
                adminCollection.updateOne({ _id }, _data).then(res => {
                    if (res.modifiedCount) {
                        resolve('修改管理员信息成功')
                    } else {
                        resolve('管理员信息保持不变')
                    }
                }).catch(err => {
                    resolve('修改管理员信息失败')
                })
            }else {
                resolve('已经存在该管理员')
            }
        })
    })
}

function removeAdmin (_id) {
    return new Promise(resolve => {
        adminCollection.deleteOne({ _id }).then(res => {
            if(res.deletedCount){
                resolve('注销管理员成功')
            }else {
                resolve('管理员已经注销')
            }
        }).catch(err => {
            resolve('注销管理员失败')
        })
    })
}

function checkAdminLogin ({ adminname, password }) {
    return new Promise((resolve, reject) => {
        adminCollection.findOne({ adminname, password }).then(res => {
            if (res) {
                resolve({
                    msg: '登录成功',
                    author: res.author,
                    type: res.type,
                    freeze:res.freeze
                })
            } else {
                reject('用户名或密码错误')
            }
        })
    })
}

module.exports = {
    insertAdmin,
    checkAdminLogin,
    getAdminList,
    freezeAdmin,
    modifyAdmin,
    removeAdmin
}
